package io.github.yotta.logger.manager.service;

import io.github.yotta.common.api.model.Paging;
import io.github.yotta.logger.api.model.LogOperationData;
import io.github.yotta.logger.api.request.LogCreateRequest;
import io.github.yotta.logger.api.request.LogPageQueryRequest;
import io.github.yotta.logger.manager.entity.LogOperation;
import io.github.yotta.logger.manager.manager.LogManager;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yotta
 * @date 2023/3/26 18:35
 */
@Service
public class LogServiceImpl implements LogService {

    private final LogManager logManager;

    public LogServiceImpl(LogManager logManager) {
        this.logManager = logManager;
    }

    @Override
    public Paging<LogOperationData> page(LogPageQueryRequest request) {
        Paging<LogOperation> page = logManager.page(request);
        return page.map(this::covert);
    }

    @Override
    public Long save(LogCreateRequest request) {
        final LogOperation convert = convert(request);
        logManager.save(convert);
        return convert.getId();
    }

    @Override
    public List<LogOperationData> list(LogPageQueryRequest request) {
        List<LogOperation> logOperations = logManager.list(request);
        if (CollectionUtils.isEmpty(logOperations)) {
            return Collections.emptyList();
        }
        return logOperations.stream().map(this::covert).collect(Collectors.toList());
    }

    private LogOperation convert(LogCreateRequest request) {
        LogOperation logOperation = new LogOperation();
        logOperation.setTenantId(request.getTenantId());
        logOperation.setApplication(request.getApplication());
        logOperation.setName(request.getName());
        logOperation.setModel(request.getModel());
        logOperation.setRequestJson(request.getRequestJson());
        logOperation.setResponseJson(request.getResponseJson());
        logOperation.setOccurTime(request.getOccurTime());
        logOperation.setOperatorId(request.getOperatorId());
        logOperation.setOperatorName(request.getOperatorName());
        logOperation.setIsSuccess(request.getIsSuccess());
        logOperation.setMessage(request.getMessage());
        logOperation.setBizId(request.getBizId());
        return logOperation;
    }


    private LogOperationData covert(LogOperation logOperation) {
        if (Objects.isNull(logOperation)) {
            return null;
        }
        final LogOperationData logOperationData = new LogOperationData();
        logOperationData.setApplication(logOperation.getApplication());
        logOperationData.setTenantId(logOperation.getTenantId());
        logOperationData.setName(logOperation.getName());
        logOperationData.setModel(logOperation.getModel());
        logOperationData.setRequestJson(logOperation.getRequestJson());
        logOperationData.setResponseJson(logOperation.getResponseJson());
        logOperationData.setOccurTime(logOperation.getOccurTime());
        logOperationData.setOperatorId(logOperation.getOperatorId());
        logOperationData.setOperatorName(logOperation.getOperatorName());
        logOperationData.setIsSuccess(logOperation.getIsSuccess());
        logOperationData.setMessage(logOperation.getMessage());
        logOperationData.setBizId(logOperation.getBizId());
        return logOperationData;
    }
}
